package com.bill.common.news.action;

import com.bill.common.news.dto.CategoryDTO;
import com.bill.common.news.dto.NewsContentDTO;
import com.bill.common.news.dto.NewsDTO;
import com.bill.common.news.dto.NewsDetailDTO;
import com.bill.common.news.dto.NewsGridDTO;
import com.bill.common.news.dto.NewsQueryDTO;
import com.bill.common.news.dto.NewsWebDTO;
import com.bill.common.news.dto.ViewNewListDTO;
import com.bill.common.news.service.ICategoryService;
import com.bill.common.news.service.INewsService;
import com.opensymphony.xwork2.ActionSupport;
import com.sinba.common.dto.PageDTO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings( "serial" )
public class NewsAction
    extends ActionSupport
{
    @Autowired
    private INewsService newsService;
    @Autowired
    private ICategoryService categoryService;
    private PageDTO pageDTO = new PageDTO(  );
    private NewsGridDTO newsGridDTO = new NewsGridDTO(  );
    private NewsDTO newsDTO = new NewsDTO(  );
    private NewsDetailDTO newsDetailDTO = new NewsDetailDTO(  );
    private NewsQueryDTO newsQueryDTO = new NewsQueryDTO(  );
    private Long[] ids;
    private String sord;
    private String sidx;
    private int page = 1;
    private int rows = 10;
    private ViewNewListDTO viewNweListDTO = new ViewNewListDTO(  );
    private List<CategoryDTO> categorysDTO = new ArrayList<CategoryDTO>(  );
    private NewsWebDTO newsWebDTO = new NewsWebDTO(  );
    private NewsContentDTO newContentDTO = new NewsContentDTO(  );

    public PageDTO getPageDTO(  )
    {
        return pageDTO;
    }

    public void setPageDTO( PageDTO pageDTO )
    {
        this.pageDTO = pageDTO;
    }

    public NewsGridDTO getNewsGridDTO(  )
    {
        return newsGridDTO;
    }

    public void setNewsGridDTO( NewsGridDTO newsGridDTO )
    {
        this.newsGridDTO = newsGridDTO;
    }

    public NewsDTO getNewsDTO(  )
    {
        return newsDTO;
    }

    public void setNewsDTO( NewsDTO newsDTO )
    {
        this.newsDTO = newsDTO;
    }

    public NewsDetailDTO getNewsDetailDTO(  )
    {
        return newsDetailDTO;
    }

    public void setNewsDetailDTO( NewsDetailDTO newsDetailDTO )
    {
        this.newsDetailDTO = newsDetailDTO;
    }

    public Long[] getIds(  )
    {
        return ids;
    }

    public void setIds( Long[] ids )
    {
        this.ids = ids;
    }

    public String getSord(  )
    {
        return sord;
    }

    public void setSord( String sord )
    {
        this.sord = sord;
    }

    public String getSidx(  )
    {
        return sidx;
    }

    public void setSidx( String sidx )
    {
        this.sidx = sidx;
    }

    public int getPage(  )
    {
        return page;
    }

    public void setPage( int page )
    {
        this.page = page;
    }

    public int getRows(  )
    {
        return rows;
    }

    public void setRows( int rows )
    {
        this.rows = rows;
    }

    public NewsQueryDTO getNewsQueryDTO(  )
    {
        return newsQueryDTO;
    }

    public void setNewsQueryDTO( NewsQueryDTO newsQueryDTO )
    {
        this.newsQueryDTO = newsQueryDTO;
    }

    public List<CategoryDTO> getCategorysDTO(  )
    {
        return categorysDTO;
    }

    public void setCategorysDTO( List<CategoryDTO> categorysDTO )
    {
        this.categorysDTO = categorysDTO;
    }

    public ViewNewListDTO getViewNweListDTO(  )
    {
        return viewNweListDTO;
    }

    public void setViewNweListDTO( ViewNewListDTO viewNweListDTO )
    {
        this.viewNweListDTO = viewNweListDTO;
    }

    public NewsWebDTO getNewsWebDTO(  )
    {
        return newsWebDTO;
    }

    public void setNewsWebDTO( NewsWebDTO newsWebDTO )
    {
        this.newsWebDTO = newsWebDTO;
    }

    public NewsContentDTO getNewContentDTO(  )
    {
        return newContentDTO;
    }

    public void setNewContentDTO( NewsContentDTO newContentDTO )
    {
        this.newContentDTO = newContentDTO;
    }

    public String findNewsPager(  )
    {
        int start = ( page - 1 ) * rows;
        newsQueryDTO.setStart( start );
        newsQueryDTO.setLimit( rows );
        pageDTO = newsService.findNewsPager( newsQueryDTO, sord, sidx );

        if ( pageDTO != null )
        {
            pageDTO.setPage( page );
            pageDTO.setRows( rows );
        }

        return SUCCESS;
    }

    /**
     *  查询到页�?
             * @return
     */
    public String findNewsPagerTopage(  )
    {
        int start = ( page - 1 ) * rows;
        newsQueryDTO.setStart( start );
        newsQueryDTO.setLimit( rows );
        pageDTO = newsService.findNewsPager( newsQueryDTO, sord, sidx );

        if ( pageDTO != null )
        {
            pageDTO.setPage( page );
            pageDTO.setRows( rows );
        }

        return "newsList";
    }

    public String saveNews(  )
    {
        newsService.saveNews( newsDTO );

        return SUCCESS;
    }

    public String editNews(  )
    {
        newsService.editNews( newsDTO );

        return SUCCESS;
    }

    public String deleteNews(  )
    {
        newsService.delteNews( ids );

        return SUCCESS;
    }

    public String findNewsById(  )
    {
        newsDetailDTO = newsService.findNewsById( newsQueryDTO.getNewId(  ) );

        return "editNewsPage";
    }

    public String findNewsByNewType(  )
    {
        newsDetailDTO = newsService.findNewsByNewType( newsQueryDTO.getNewType(  ) );

        return "editNewsPage";
    }

    public String newsInfo(  )
    {
        if ( newsQueryDTO.getNewId(  ) != null )
        {
            newsDetailDTO = newsService.findNewsById( newsQueryDTO.getNewId(  ) );
        }

        if ( StringUtils.hasText( newsQueryDTO.getNewType(  ) ) )
        {
            newsDetailDTO = newsService.findNewsByNewType( newsQueryDTO.getNewType(  ) );
        }

        return "newsInfo";
    }

    public String newsInfoOut(  )
    {
        if ( newsQueryDTO.getNewId(  ) != null )
        {
            newsDetailDTO = newsService.findNewsById( newsQueryDTO.getNewId(  ) );
        }

        if ( StringUtils.hasText( newsQueryDTO.getNewType(  ) ) )
        {
            newsDetailDTO = newsService.findNewsByNewType( newsQueryDTO.getNewType(  ) );
        }

        return "newsInfoOut";
    }

    public String aboutInfo(  )
    {
        CategoryDTO qdto = new CategoryDTO(  );
        qdto.setCategoryCode( newsQueryDTO.getCategoryCode(  ) );
        categorysDTO = categoryService.findCategoryPager( qdto, "desc", "categoryOrder" ).getData(  );
        newsQueryDTO.setCategoryId( categorysDTO.get( 0 ).getCategoryId(  ) );
        newsDetailDTO = newsService.findNewsBycode( newsQueryDTO, "", "" );

        return "aboutInfo";
    }

    public String newsm(  )
    {
        viewNweListDTO = newsService.findNewsAndCategorysByCategoryId( newsQueryDTO, sord, sidx );

        return "news";
    }

    public String findTop1NewsByCategory(  )
    {
        newContentDTO = newsService.findTop1NewsByCategory( newsQueryDTO );

        return "newContentDTO";
    }

    public String findNewsByCategory(  )
    {
        pageDTO = newsService.findNewsByCategory( newsQueryDTO );

        if ( pageDTO != null )
        {
            pageDTO.setPage( page );
            pageDTO.setRows( rows );
        }

        return SUCCESS;
    }
}
